package com.launcher.minecraft;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;

public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.AccountViewHolder> {

    private List<AccountManagerActivity.Account> accounts;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(AccountManagerActivity.Account account);
        void onDeleteClick(AccountManagerActivity.Account account);
    }

    public AccountAdapter(List<AccountManagerActivity.Account> accounts, OnItemClickListener listener) {
        this.accounts = accounts;
        this.listener = listener;
    }

    @NonNull
    @Override
    public AccountViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_account, parent, false);
        return new AccountViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AccountViewHolder holder, int position) {
        AccountManagerActivity.Account account = accounts.get(position);
        holder.bind(account);
    }

    @Override
    public int getItemCount() {
        return accounts.size();
    }

    class AccountViewHolder extends RecyclerView.ViewHolder {
        private TextView tvUsername;
        private TextView tvAccountType;
        private Button btnDelete;
        private View rootView;

        public AccountViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvAccountType = itemView.findViewById(R.id.tv_account_type);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            rootView = itemView.findViewById(R.id.root_view);
        }

        public void bind(final AccountManagerActivity.Account account) {
            tvUsername.setText(account.getUsername());
            
            if (account.getType() == AccountManagerActivity.Account.TYPE_OFFLINE) {
                tvAccountType.setText("离线账号");
            } else if (account.getType() == AccountManagerActivity.Account.TYPE_MICROSOFT) {
                tvAccountType.setText("微软账号");
            }
            
            // Highlight selected account
            if (account.isSelected()) {
                rootView.setBackgroundColor(itemView.getContext().getResources().getColor(R.color.colorPrimary));
            } else {
                rootView.setBackgroundColor(itemView.getContext().getResources().getColor(android.R.color.transparent));
            }
            
            // Set click listeners
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(account);
                    }
                }
            });
            
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onDeleteClick(account);
                    }
                }
            });
        }
    }
}
